<?php

$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
set_include_path(get_include_path().PATH_SEPARATOR.$baseDir.PATH_SEPARATOR.$baseDir.'/incubator');

include '../../testingfunctions.php';
include 'Pone/Pone_Exception.php';
include 'Pone/Database/Pone_Database_ConnectionFactory.php';
include_once './dbconfig.php'; // $config is defined here
include_once './test_setup.php'; // refreshTestData() is defined here

try
{
    refreshTestData($config);
    // /home/web/windows.php.net/docroot/downloads/snaps/
    $conn = Pone_Database_ConnectionFactory::getConnection($config);
    $stmt = $conn->createStatement();

    $updateData  = array(
      'username' => 'pcdinh4.1',
    );

    // Conditions as an array
    $updateCond  = array(
      'id'       => '= 4',
    );

    $affectedRowCount = $stmt->update('test2', $updateData, $updateCond);
    assertTrue($affectedRowCount === 1, 'Expected: One row is updated. ');
    echo '<br />';
    $currentData = $stmt->fetchOne('SELECT username FROM test2 WHERE id = 4');
    assertTrue($currentData === 'pcdinh4.1', 'Expected: User who has an ID of 4 has his username updated to '.$currentData);
    echo '<br />';
    $updateData  = array(
      'username' => 'pcdinh4',
    );

    // Conditions as a string
    $updateCond  = 'id = 4';

    $affectedRowCount = $stmt->update('test2', $updateData, $updateCond);
    $currentData = $stmt->fetchOne('SELECT username FROM test2 WHERE id = 4');
    assertTrue($currentData === 'pcdinh4', 'Expected: User who has an ID of 4 has his username re-updated to '.$currentData);
    // Free resources
    $stmt->close();
}
catch (Pone_Exception_DatabaseAccess $ex)
{
    echo implode('. ', $ex->getTraceMessages());
    echo '<br />';
    echo $ex->getNativeMessage();
}
catch (Exception $ex)
{
    echo $ex->getMessage();
}

$conn->close();
?>